<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/04/0004
 * Time: 10:52
 */

namespace app\sports_admin\service;


use app\exception\TeacherException;
use think\Request;
use app\sports_admin\model\Teacher as TeacherModel;
class Teacher
{
    /**
     * 添加教师
     * @param $teacherName 教师名称
     * @param $teacherPhone 联系方式(登录账号)
     * @param $password 登录密码
     * @param $tid 职工号
     * @param $schoolId 学校id
     */
    public static function teacherInsert()
    {
        $params = Request::instance()->only(['teacher_name','teacher_phone','pwd','tid','school_id']);

        if(TeacherModel::where('teacher_phone',$params['teacher_phone'])->find()){
            throw new TeacherException([
                'msg'=>'手机号已存在',
                'errorCode'=>400
            ]);
        }
        $params['pwd'] = authcode($params['pwd'],'ENCODE',$params['teacher_phone'],0);
        $teacherId = TeacherModel::create($params);
        if(!$teacherId){
            throw new TeacherException([
                'msg'=>'教师信息添加失败',
                'errorCode'=>400
            ]);
        }
        return json(['code'=>201,'msg'=>'添加成功']);
    }
    /**
     * 添加教师
     * @param $teacherName 教师名称
     * @param $tid 职工号
     * @param $id 教师id
     */
    public static function teacherUpdate()
    {
        $params = Request::instance()->only(['teacher_name','tid']);
        $id = input('post.id');
        $teacherId = TeacherModel::where('id',$id)->update($params);
        if(!$teacherId){
            throw new TeacherException([
                'msg'=>'教师信息修改失败',
                'errorCode'=>400
            ]);
        }
        return json(['code'=>201,'msg'=>'修改成功']);
    }

}